package m.toolkit.controller;

import javax.servlet.http.HttpServletRequest;

import m.framework.controller.BaseController;
import m.framework.utils.StringUtil;
import m.toolkit.console.Command;
import m.toolkit.console.CommandPool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;

/**
 * 控制台,执行shell命令,将结果输出到页面
 * @author ming
 * @date 2014年2月22日下午9:48:44
 */
@Controller
public class ConsoleController extends BaseController{
	private static Logger log = LoggerFactory.getLogger(ConsoleController.class);
	private static CommandPool pool=new CommandPool();
	
	/**执行某个命令*/
	public Command command(Command command){
		command.setCommandId(StringUtil.uuid());
		pool.addCommand(command);
		return command;
	}
	
	/**获得命令执行的最新状态*/
	public Command  getMessage(HttpServletRequest request){
		String commandId=request.getParameter("commandId");
		String lastUpdateTimeStr=request.getParameter("lastUpdateTime");
		Long time=0L;
		if(!StringUtil.isEmpty(lastUpdateTimeStr)){
			time=Long.parseLong(lastUpdateTimeStr);
		}
		return pool.getOutput(commandId, time);
	}
	
	/**取消命令*/
	public void stop(Command command){
		pool.stop(command.getCommandId());
	}
}




